<template>
  <view>
    <router-view/>
    <!-- 全局登录弹窗组件 -->
    <loginwx ref="loginComponent"></loginwx>
  </view>
</template>

<script>
	import storageUtil from '@/common/util/storageUtil.js';
	import api from '@/common/api/webapi.js'; /*api*/
	import QQMapWX from '@/libs/util/qqmap-wx-jssdk.js'
	import loginwx from '@/components/login-wx/loginwx.vue'
	export default {
		components: {
			loginwx
		},
		data() {
			return {
				// 组件相关数据可以在这里定义
			};
		},
		globalData: {
			statusBarHeight: 0, // 状态导航栏高度
			navHeight: 0, // 总体高度
			navigationBarHeight: 0, // 导航栏高度(标题栏高度)
			navTop: 0,
			qqmapsdk: null,
		},
		onLaunch: function(options) {
			console.log("app",options);
			
			// 状态栏高度
			this.globalData.statusBarHeight = uni.getSystemInfoSync().statusBarHeight
			// #ifdef MP-WEIXIN
			// 获取微信胶囊的位置信息 width,height,top,right,left,bottom
			const custom = wx.getMenuButtonBoundingClientRect()
			// console.log(custom)
			// console.log('navBarHeight' + this.$navBarHeight);

			// 导航栏高度(标题栏高度) = 胶囊高度 + (顶部距离 - 状态栏高度) * 2
			this.globalData.navigationBarHeight = custom.height + (custom.top - this.globalData.statusBarHeight) * 2
			// console.log("导航栏高度："+this.globalData.navigationBarHeight)

			// 总体高度 = 状态栏高度 + 导航栏高度
			this.globalData.navHeight = this.globalData.navigationBarHeight + this.globalData.statusBarHeight;
			this.$navBarHeight = this.globalData.navigationBarHeight;
			this.globalData.navTop = this.globalData.statusBarHeight;
			this.globalData.qqmapsdk = new QQMapWX({
				key: "3H3BZ-FGKKV-7XGP6-5ZHFX-MJYCO-7OFBW"
			});
			// #endif
			// console.log('小程序状态栏：' +JSON.stringify(this.globalData) )
			// console.log('App Launch')
			//获取微信扫码进入小程序带来的url,对url进行解码，获取解码后的地址及参数
			//如果小程序没有登陆，跳转至登陆
			//登陆后带着参数再回到页面
			//判断是否登陆
			let _this = this; 

			wx.checkSession({
				success: (e) => {
					let userLoginFlag = storageUtil.getStorageData("isLogin");

					if (!userLoginFlag || userLoginFlag == '') {
						storageUtil.removeStorageData("isLogin");
						storageUtil.removeAllUserData();
						_this.wxLogin();

					}

				},
				fail: () => {
					// console.log('登录已过期');

					//没用到token可以直接忽略以下步骤:
					//没登录就异步删除验证token
					storageUtil.removeStorageData("isLogin");
					storageUtil.removeAllUserData();
					_this.wxLogin();
				}
			});
			this.getRegionList();
			// 监听token过期事件
			uni.$on('tokenExpired', this.onTokenExpired);
		},
		
		

		onShow: function(options) {
			//获取扫码打开小程序，传参的数据
			// let scene = options.scene;
			// let _this = this;
			// if (scene == 1011 || scene == 1012 || scene == 1013) //扫码进入
			// {
			// 	if (decodeURIComponent(options.query)) {
			// 		let urlStr = decodeURIComponent(options.query.q);

			// 		//获取参数  
			// 		let _params = this.getUrlVars(urlStr);

			// 		let distributionCode = _params["distributionCode"];
			// 		let type = _params["type"];

			// 		if (distributionCode) {
			// 			_this.$store.commit("SET_DistributionCode", distributionCode)
			// 		}
			// 		let userLoginFlag = storageUtil.getStorageData("isLogin");
			// 		if (type == "reg") {
			// 			if (!userLoginFlag || userLoginFlag == '') {
			// 				_this.$store.commit("SET_afterloginUrl", '/pages/Join/Join');

			// 			} else {
			// 				let paransData = {
			// 					distributionCode: distributionCode
			// 				}
			// 				uni.navigateTo({
			// 					url: "/pages/Join/Join" + "?param=" + encodeURI(JSON.stringify(paransData))
			// 				});
			// 			}
			// 		}
			// 	}

			// } else {
			// 	_this.$store.commit("SET_DistributionCode", "")
			// }


		},
		onLoad: function(options) {
			console.log("onLoad",options);
		},

		onHide: function() {
				console.log('App Hide')
			},
		onUnload() {
			// 移除事件监听
			uni.$off('tokenExpired', this.onTokenExpired);
		},
		methods: {
			onTokenExpired() {
				// 显示登录弹窗
				this.$refs.loginComponent.open();
			},
			wxLogin() {
				let _this = this
				uni.login({
					provider: 'weixin',
					success: function(res) {
						if (res.code) {
							storageUtil.saveStorageData('login_code', res.code);
							console.log("获取微信code" + res.code)
						} else {

							_this.wxLogin()
						}
					},
					fail: (res) => {
						_this.wxLogin()
					}
				})
			},

			getUrlVars(href) {
				var vars = {};
				var parts = href.replace(/[?&]+([^=&]+)=([^&]*)/gi, function(m, key, value) {
					vars[key] = value;
				});
				return vars;
			},
			getRegionList(){
				api.getRegionList().then(res => {
					if (res.code === 0) {
						console.log(res.data.length);
						if (res.data.length > 0) {
							storageUtil.saveStorageData("region",JSON.stringify(res.data));
						}
					} else {}
				}).catch()
			},
		}
	}
</script>

<style lang="scss">
	/*每个页面公共css */
	@import '@/uni.scss';
	/* #ifndef APP-NVUE */
	@import '@/static/basic.scss';
	@import '@/static/customicons.css';
	@import '@/static/common.scss';
	// 设置整个项目的背景色
	page {
		background-color: $uni-color-white;
		font-size: $uni-font-size-28;
	}
	html,body{
		overflow: hidden;
	}
	
	.uni-padding-wrap {
		padding: 0 15px;
	}

	/* #endif */
	.example-info {
		font-size: 14px;
		color: #333;
		padding: 10px;
	}
</style>
